#include <avz.h>
#include "helium/helium_helper_se.h"
#include "SimpleAvZ/lib.h"
#include <vector>

using namespace AvZ;

void C_5(int time, ShovelTime shovel_time)
{
    auto zombie_array = GetMainObject()->zombieArray();
    int num[5] = {0, 0, 0, 0, 0};
    int row;
    for (int index = 0; index < GetMainObject()->zombieTotal(); index++)
    {
        if (zombie_array[index].isExist() && !zombie_array[index].isDead() && (zombie_array[index].type() == JACK_IN_THE_BOX_ZOMBIE || zombie_array[index].type() == LADDER_ZOMBIE))
        {
            row = zombie_array[index].row();
            num[row] += 1;
        }
    }

    int index = 4;
    int max = num[0];
    for (int i = 0; i < 4; i++)
    {
        if (num[i] > max)
        {
            max = num[i];
            index = i;
        }
    };

    C(time, shovel_time, POT, index + 1, 9);
    std::vector<int> vector_ = {1, 2, 3, 4, 5};
    vector_.erase(vector_.begin() + index);
    C(time, shovel_time, {PUFF, SUN, SCAREDY, SUNFLOWER}, vector_, 9);
}

void Pd1_PD() // 收上波一路跳跳, 漏一路跳跳, 上波PP
{
    P(581, 2, 9);
    D(after(360), 1, 4);
    P(410, 4, 8.575);
    D(after(220), 4, 8.575);
}
void Pd2_PD() // 收上波三路跳跳, 漏一路跳跳, 上波PP
{
    P(581, 2, 9);
    D(after(362), 2, 4);
    P(410, 4, 8.575);
    D(after(220), 4, 8.575);
}
void JAD_Pd() // 收上波一路跳跳, 漏三路跳跳, 上波DD
{
    J(366 - 107, 1, 9);
    A(366, 1, 9);
    D(622, 1, 8.0625);
    P(581, 4, 9);
    D(after(362), 4, 4);
}
void PPdd_594() // 漏一路跳跳
{
    PP(594, 8.625);
    DD(after(374), 3.825);
}

void Script()
{
    SeedSlot(10);                                    // 10 卡槽
    OpenMultipleEffective('Q', MAIN_UI_OR_FIGHT_UI); // 反复重开
    SetAdvancedPauseKey('Z');                        // 高级暂停
    SetSkipTickKey('S', 9);                          // 跳帧
    SetSkipTickKey('D', 19);
    // SetErrorMode(CONSOLE);

    int length_1 = 1514;
    int length_2 = 1466;

    SetZombies_AllType(DE);
    SelectCards({
        ICE_SHROOM,     // I
        M_ICE_SHROOM,   // I'
        COFFEE_BEAN,    // 咖啡豆
        CHERRY_BOMB,    // A
        JALAPENO,       // A'
        PUFF_SHROOM,    // c
        SUN_SHROOM,     // c
        SCAREDY_SHROOM, // c
        FLOWER_POT,     // c
        SUNFLOWER,      // c
    });

    SetIce({{1, 6}, {2, 6}, {3, 6}, {4, 6}, {5, 6}});
    MaidOn();

    for (auto w : waves(1))
    {
        // PPDDA'+5c
        PP(225);
        DD(after(107), 8.175);
        J(401, 3, 9);
        C_5(599, until(601 + 341 - 207));

        EnsureExist({GIGA, 3});
    }

    for (auto w : waves(2))
    {
        // PPSADD
        PP(284);
        P(291, 2, 9);
        A(294, 4, 9);
        DD(401, 8.8);
        RM(193, 4, 9); // 防止留存花盆导致樱桃瞬爆

        I(601);
    }

    for (auto w : waves(3))
    {
        // IPP+c+c-PP 1530
        PP(301, 9);
        C_5(148, until(1000));
        C_5(1000, until(1330));
        PP(1330, 9);

        I(1530 + 10);

        EnsureExist({POGO_ZOMBIE, 1});
    }

    for (auto w : waves(4))
    {
        // I3PPdd-PP 1507
        PPdd_594();
        C_5(848, until(length_1 - 200 - 181));
        PP(length_1 - 200, 9);

        I(length_1 + 10);

        EnsureExist({POGO_ZOMBIE, 3});
    }

    for (auto w : waves(5))
    {
        // I3PD/Pd-DD
        Pd1_PD();
        C_5(800, until(length_1 - 200 - 181));
        DD(length_1 - 200, 9);

        I(length_1 + 10);

        EnsureExist({GIGA, 2});
    }

    for (auto w : waves(6, 12))
    {
        // I3A'AD/Pd-PP
        JAD_Pd();
        C_5(800, until(length_2 - 200 - 181));
        PP(length_2 - 200, 9);

        I(length_2 + 10);

        EnsureExist({DIGGER_ZOMBIE, 2});
        EnsureExist({POGO_ZOMBIE, 3});
    }

    for (auto w : waves(7, 8, 13, 14, 15, 16, 17, 18))
    {
        // I3PD/Pd-PP
        if (w != 7 && w != 13)
            Pd1_PD();
        else
            Pd2_PD();
        C_5(872, until(length_1 - 200 - 181));
        PP(length_1 - 200, 9); // 一路漏撑杆？？

        I(length_1 + 10);

        if (w == 8)
            EnsureExist({POGO_ZOMBIE, 1});
    }

    for (auto w : waves(9, 19))
    {
        // I3PD/Pd-PDd/PAdA'-PP-D/0-0/D
        Pd1_PD();
        C_5(872, until(length_1 - 200 - 181));
        PP(1434 - 210, 8.6);
        A(1434, 4, 9); // 最早举锤445*2-10+399=1279
        D(1434, 2, 9);
        DD(after(334), 5.175);
        J(1434 + 414 + 212, 4, 9);
        RM(after(-101), 4, 9);

        C(872 + 751, keep(751), {PUFF, SUN}, {1, 2}, 9);
        C(after(751), keep(751), {PUFF, SUN}, {1, 2}, 9);
        C(872 + 751, keep(268), POT, 5, 9);
        C(after(751), keep(268), SUNFLOWER, 5, 9);
        C(after(207), keep(134), SCAREDY, 5, 9);
        C(after(207), keep(134), POT, 5, 9);

        PP(2549);
        P(2598, 2, 9);
        P(2923, 4, 9);

        if (w == 9)
            SetIce(0, {{1, 6}, {2, 6}, {3, 6}, {3, 9}});
        EnsureExist({GIGA, 1, 2, 4, 5});
        EnsureExist({POGO_ZOMBIE, 1});
    }

    for (auto w : waves(10))
    {
        // PPDDdd
        PP(282); // 投石车
        PP(after(102));
        DD(after(227), 4.5);

        I(601);
        SetIce(602, {{1, 6}, {2, 6}, {3, 6}, {4, 6}, {5, 6}});

        EnsureExist({POGO_ZOMBIE, 1});
        EnsureExist({POLE_VAULTING_ZOMBIE, 3});
    }

    for (auto w : waves(11))
    {
        // IPP-PP
        PP(301, {2, 4}, 9); // 冰车碾9小喷
        C_5(148, until(1000));
        C_5(1000, until(1307 - 181 - 1)); // 最早434*2+399=1267举锤
        DD(length_1 - 200, 9);
        I(length_1 + 10);

        EnsureExist({GIGA, 2});
    }

    for (auto w : waves(20))
    {
        MaidMove(0);
        MaidOn(500);
        MaidMove(1000);
        MaidOn(1200);

        PP(282); // 投篮压9普283
        PP(318);
        DD(318);
        DD(318 + 110);
        PP(813 + 22);

        PP(600, 4.5);

        P(5500, 3, 9);
    }

    // 偷花
    SetTime(800 + 751, 5);
    Card(SUNFLOWER, 5, 6);
    SetTime(109, 8); // 872c
    Card(SUNFLOWER, 4, 6);
    SetTime(105, 11);
    Shovel(4, 6);

    SetTime(85, 13);
    Card(SUNFLOWER, 4, 6); // 872c
    SetTime(109, 17);
    Card(SUNFLOWER, 3, 6); // 872c
    SetTime(4583, 19);
    Shovel(3, 6);
    SetTime(2799, 20);
    Shovel(4, 6);
    SetTime(4339, 20);
    Shovel(5, 6);

    SetTime(872 + 751, 19);
    Card(SUNFLOWER, 3, 9);
    Delay(751 * 2);
    Card(SUNFLOWER, 1, 9);
    Delay(751);
    Card(SUNFLOWER, 2, 9);
    Delay(751);
    Card(SUNFLOWER, 4, 9);
    Delay(751);
    Card(SUNFLOWER, 5, 9);

    SetTime(5998, 20);
    Shovel({{1, 9}, {2, 9}, {3, 9}, {4, 9}, {5, 9}});
}